<?php

class Option extends CI_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('option_model');
		$this->load->library('upload');
		$this->load->helper(array('form', 'url'));
		$this->load->library('form_validation');
		//$this->load->library('security');
		$this->load->library('auth/tank_auth');
		$this->lang->load('auth/tank_auth');
		$this->load->model('tank_auth/users');
	}
	
	function index(){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				$data['hasil'] = $this->option_model->getAll();
				//$this->load->view('option',$data);
				$data['content'] = 'option';
				$this->load->view('common/common',$data);
			}else{
				redirect('/common/');
			}
		}
	}
	
	function add(){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				if($_POST == NULL) {
						//$this->load->view('option_form');
						$data['content'] = 'option_form';
						$this->load->view('common/common',$data);
				}else {
						$config['upload_path'] = './data/upload/';
						$config['allowed_types'] = 'jpg|jpeg|png|gif';
						$config['max_size'] = '5000';							
						$this->load->library('upload', $config);
						$this->upload->initialize($config);
						if (!$this->upload->do_upload('image')){
							//echo $this->upload->display_errors(); 
							$this->option_model->create($_POST);
						}else{
							//$upload_data = array('upload' => $this->upload->data());
							$upload_data = $this->upload->data();
							//print_r($this->upload->data());
							
							// create thumbnail
								if (isset($upload_data['full_path']) && $upload_data['full_path'] != ''){
									$this->load->library('image_lib');
									$config = array('source_image' => $upload_data['full_path'], 
													'new_image' => './data/thumb/', 
													'maintain_ration' => true, 
													'width' => 150,
					            					'height' => 130);
					
									$this->image_lib->initialize($config);
									$this->image_lib->resize();
									$this->image_lib->clear();
								}
							
							$this->option_model->create($_POST);
						}
						redirect('option');
				}
			}else{
				redirect('/common/');
			}
		}
				
          
    }
	
	function updatedata($product_option_id){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				if($_POST == NULL){
					$data['hasil'] =  $this->option_model->select($product_option_id);
					//$this->load->view('option_form_update', $data);
					$data['content'] = 'option_form_update';
					$this->load->view('common/common',$data);
				}else{
						$config['upload_path'] = './data/upload/';
						$config['allowed_types'] = 'jpg|jpeg|png|gif';
						$config['max_size'] = '5000';							
						$this->load->library('upload', $config);
						$this->upload->initialize($config);
						if (!$this->upload->do_upload('image')){
							//echo $this->upload->display_errors();
							$this->option_model->update($product_option_id);
						}else{
								$config = $this->option_model->get_option_by_id($product_option_id);
								if($config['image'] == NULL){
									$upload_data = $this->upload->data();
									//print_r($this->upload->data());
									
									// create thumbnail
									if (isset($upload_data['full_path']) && $upload_data['full_path'] != ''){
										$this->load->library('image_lib');
										$config = array('source_image' => $upload_data['full_path'], 
														'new_image' => './data/thumb/', 
														'maintain_ration' => true, 
														'width' => 150,
					            						'height' => 130);
						
										$this->image_lib->initialize($config);
										$this->image_lib->resize();
										$this->image_lib->clear();
									}
									
									$this->option_model->update($product_option_id);
								}else{
									unlink ('./data/upload/'.$config['image']); 
									unlink ('./data/thumb/'.$config['image']);
									$upload_data = $this->upload->data();
									//print_r($this->upload->data());
									
									// create thumbnail
									if (isset($upload_data['full_path']) && $upload_data['full_path'] != ''){
										$this->load->library('image_lib');
										$config = array('source_image' => $upload_data['full_path'], 
														'new_image' => './data/thumb/', 
														'maintain_ration' => true, 
														'width' => 150,
					            						'height' => 130);
						
										$this->image_lib->initialize($config);
										$this->image_lib->resize();
										$this->image_lib->clear();
									}
									
									$this->option_model->update($product_option_id);
								}
								
							}
							//$this->option_model->update($product_option_id);
						
						//$upload_data = $this->upload->data();
						//$this->option_model->update($product_option_id);
						redirect('option');
				}
			}else{
				redirect('/common/');
			}
		}
    }
	
	function deletedata($product_option_id){
		if (!$this->tank_auth->is_logged_in()){	
			redirect('/auth/login/');
		}else{
			if ($this->tank_auth->get_url()){
				$config = $this->option_model->get_option_by_id($product_option_id);
				if($config['image'] == NULL){
					$this->option_model->delete($product_option_id);
					redirect('option');
				}else{
					unlink ('./data/upload/'.$config['image']); 
					unlink ('./data/thumb/'.$config['image']); 
					$this->option_model->delete($product_option_id);
					redirect('option');
				}
			}else{
				redirect('/common/');
			}
		}
    }
	
}
?>